
/**
** \file       ast_printcmd.c
**
** \brief      this function handle print cmd
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse, De Olivera
**             Guillaume and Nguyen Viet Dung
**
** \version    0.8
**
** \date       26 Novembre 2010
**
*/

#include "ast_printer.h"

int	print_cmd(s_command	*node,
		  int		i,
		  int		fd,
		  int		last)
{
  while (node != NULL)
    {
      ++last;
      print_int(last, fd);
      write(fd, " [label = \"cmd\"];\n", 18);
      print_int(i, fd);
      write(fd, " -> ", 4);
      print_int(last, fd);
      write(fd, "\n", 1);
      if (node->smp_cmd != NULL)
	last = print_smp_cmd(node->smp_cmd, last, fd);
      else
      	{
	  if (node->sh_cmd != NULL)
	    last = print_sh_cmd(node->sh_cmd, last, fd, last);
	  if (node->funcdec != NULL)
	    last = print_funcdec(node->funcdec, last, fd, last);
	}
      node = node->next;
    }
  return (last);
}

int	print_smp_cmd(s_smp_cmd		*node,
		      int		i,
		      int		fd)
{
  int	last = i;

  ++last;
  print_int(last, fd);
  write(fd, " [label = \"simple cmd\"];\n", 25);
  print_int(i, fd);
  write(fd, " -> ", 4);
  print_int(last, fd);
  write(fd, "\n", 1);

  if (node->prefix != NULL)
    last = print_prefix(node->prefix, last, fd);
  if (node->elt != NULL)
    last = print_element(node->elt, last, fd);
  return (last);
}

int	print_sh_cmd(s_sh_cmd		*node,
		     int		i,
		     int		fd,
		     int		last)
{
  ++last;
  print_int(last, fd);
  write(fd, " [label = \"shcmd\"];\n", 20);
  print_int(i, fd);
  write(fd, " -> ", 4);
  print_int(last, fd);
  write(fd, "\n", 1);

  if (node->redir != NULL)
    last = print_redir(node->redir, last, fd);

  if (node->cmpdlist != NULL)
    last = print_cmpdlist(node->cmpdlist, i + 1, fd, last);

  if (node->rulefor != NULL)
    last = print_rulefor(node->rulefor, i + 1, fd, last);

  if (node->ruleif != NULL)
    last = print_ruleif(node->ruleif, i + 1, fd, last);

  if (node->ruleuntil != NULL)
    last = print_ruleuntil(node->ruleuntil, i + 1, fd);

  if (node->rulecase != NULL)
    last = print_rulecase(node->rulecase, i + 1, fd);

  if (node->rulewhile != NULL)
    last = print_rulewhile(node->rulewhile, i + 1, fd, last);

  return (last);
}

int	print_funcdec(s_funcdec		*node,
		      int		i,
		      int		fd,
		      int		last)
{
  if (node != NULL)
    {
        ++last;
	print_int(last, fd);
	write(fd, " [label = \"shcmd\"];\n", 20);
	print_int(i, fd);
	write(fd, " -> ", 4);
	print_int(last, fd);
	write(fd, "\n", 1);
	if (node->sh_cmd != NULL)
	  i = print_sh_cmd(node->sh_cmd, i, fd, i);
	if (node->redir != NULL)
	  last = print_redir(node->redir, i, fd);
    }
  return (last);
}

